#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_TEXT 512

struct msg_st
{
	long msg_type;
	char content[MAX_TEXT];
};

struct msg_st info;
int msgid;
	
	long al_sno;
	char ac_sname[10];
	long al_ssex;
	char ac_sbirth[10];
	char ac_sdept[10];
	
	long  al_cno;
	char ac_cname[10];
	long al_ccredit;

        long al_grade;

	char s_tmp[20];
	char c_tmp;

void toString();
void welcome();

void student_info();
void alter_stu();
void find_stu();
void student();

void course_info();
void add_course();
void del_course();
void check_course();
void course();

void add_score();
void del_score();
void alter_score();
void find_score();
void score();

int main()
{
	int c;
	int flag;

	info.msg_type = 1;

	msgid = msgget((key_t)1234,0666|IPC_CREAT);
	if(msgid == -1)
	{
		fprintf(stderr,"msgget failed\n");
		exit(EXIT_FAILURE);
	}

	flag=0;
	while(1)
	{	
		welcome();
		scanf("%d",&c);
	
		switch(c)
		{
		case 1:student();break;
		case 2:course();break;
		case 3:score();break;
		default:flag=1;break;
		}
		if(1==flag)
			break;
	}

	return 0;
}

void toString(long num,char *str)
{
	int i,k;

	i=0;
	while(1)
	{
		k = num%10;
		str[i]=k+48;
		num = num/10;
		i++;
		if(num == 0)
			break;
	}
	str[i]=0;
}


void welcome()
{
	system("clear");
	printf("Welcome to student score query system!\n");
	printf("1. Student Manage\n");
	printf("2. Course Manage\n");
	printf("3. Score Manage\n");
	printf("4. Exit\n");
	printf("Please select a option:\n");
}

void student_info()
{
	system("clear");

	//procedure: all parameter
	printf("1. Alter a student \n");
	//trigger: name
	printf("2. Find a student\n");
	printf("3. Back\n");
	printf("Please select a option:\n");
}

void alter_stu()
{
	system("clear");
	printf("Please input the student number:\n");
	scanf("%ld",&al_sno);
	printf("Please input the student new name:\n");
	scanf("%s",ac_sname);

	toString(11,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}	
	
	
	toString(al_sno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}	

	strcpy(info.content,ac_sname);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}	
	
	printf("record updated\n");
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}

void find_stu()
{
	system("clear");
	printf("Please input the student number :\n");
	scanf("%ld",&al_sno);
	
	toString(12,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
	
	toString(al_sno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}	
/*        
	EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;
	
	EXEC SQL 
		SELECT sname,ssex,sbirth,sdept 
		into :ac_sname,:al_ssex,:ac_sbirth,:ac_sdept 
		FROM student WHERE sno=:al_sno;

	
	printf("The student information:\n");
	if(1==al_ssex)
		printf("%s\tmale\t%s\t%s\n",ac_sname,ac_sbirth,ac_sdept);
	else
		printf("%s\tfemale\t%s\t%s\n",ac_sname,ac_sbirth,ac_sdept);
*/
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}


void student()
{
	int c;
	int flag;
	
	flag=0;
	while(1)
	{
		student_info();
		scanf("%d",&c);
		switch(c)
		{
		case 1:alter_stu();break;
		case 2:find_stu();break;
		default:flag=1;break;
		}
		if(1==flag)
			break;
	}
}

void course_info()
{
	system("clear");
	printf("1. Add a course\n");
	printf("2. Del a course\n");
	printf("3. Check course\n");
	printf("4. Back\n");
	printf("Please select a option:\n");
}

void add_course()
{
	system("clear");

	toString(21,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the course name:\n");
	scanf("%s",ac_cname);

	strcpy(info.content,ac_cname);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}


	printf("Please input the course credit:\n");
	scanf("%ld",&al_ccredit);

	toString(al_ccredit,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
/*
        EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;

	EXEC SQL EXECUTE
	BEGIN
		add_course(:ac_cname,:al_ccredit);
	END;
	END-EXEC;
	
	EXEC SQL COMMIT WORK RELEASE;	
*/	
	printf("Course added\n");
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}

void del_course()
{
	system("clear");


	toString(22,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the course name:\n");
	scanf("%s",ac_cname);

	strcpy(info.content,ac_cname);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}


/*	
        EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;

	EXEC SQL EXECUTE
	BEGIN
		del_course(:ac_cname);
	END;
	END-EXEC;

	EXEC SQL COMMIT WORK RELEASE;	
*/	
	printf("Course deleted\n");
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
	
}


void course()
{
	int c;
	int flag;
	flag=0;
	while(1)
	{
		course_info();
		scanf("%d",&c);
		switch(c)
		{
		case 1:add_course();break;
		case 2:del_course();break;
		case 3:check_course();break;
		default:flag=1;break;
		}
		if(1==flag)
			break;
	}
}

void score_info()
{
	system("clear");
	printf("1. Add a score\n");
	printf("2. Del a score\n");
	printf("3. Alter a score\n");
	printf("4. Find a score\n");
	printf("5. Back\n");
	printf("Please select a option:\n");
}

void add_score()
{
	system("clear");

	toString(31,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the student name:\n");
	scanf("%s",ac_sname);

	strcpy(info.content,ac_sname);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the course name:\n");
	scanf("%s",ac_cname);
	
	strcpy(info.content,ac_cname);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the score:\n");
	scanf("%ld",&al_grade);

	toString(al_grade,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
  /*      
	EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;

	EXEC SQL EXECUTE
	BEGIN
		ins_score(:ac_sname,:ac_cname,:al_grade);
	END;
	END-EXEC;
	
	EXEC SQL COMMIT WORK RELEASE;	
*/	
	printf("Score added\n");
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}

void del_score()
{
	system("clear");
	
	toString(32,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the student Number:\n");
	scanf("%ld",&al_sno);

	toString(al_sno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
	printf("Please input the course Number:\n");
	scanf("%ld",&al_cno);

	toString(al_cno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
/*
        EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;
	
	EXEC SQL DELETE FROM sc WHERE sno=:al_sno and cno=:al_cno;
	
	EXEC SQL COMMIT WORK RELEASE;	
*/	
	printf("Score deleted\n");
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}

void alter_score()
{
	system("clear");

	toString(33,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the student Number:\n");
	scanf("%ld",&al_sno);

	toString(al_sno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the course Number:\n");
	scanf("%ld",&al_cno);

	toString(al_cno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the score:\n");
	scanf("%ld",&al_grade);

	toString(al_grade,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
/*
        EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;
	
	EXEC SQL UPDATE sc SET grade=:al_grade WHERE sno=:al_sno and cno=:al_cno;

	EXEC SQL COMMIT WORK RELEASE;	
*/	
	printf("Score updated\n");
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}

void find_score()
{
	system("clear");

	toString(34,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

	printf("Please input the student Number:\n");
	scanf("%ld",&al_sno);

	toString(al_sno,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}
/*
	EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;

	EXEC SQL DECLARE score_cursor CURSOR FOR
	SELECT sname,cname,grade FROM student,course,sc
	WHERE student.sno=sc.sno
	and course.cno=sc.cno and sc.sno = :al_sno;

	printf("The Results are as follows:\n");
	
	EXEC SQL WHENEVER NOT FOUND DO break;
	
	EXEC SQL OPEN score_cursor;
	while(1)
	{
		EXEC SQL FETCH score_cursor INTO :ac_sname,:ac_cname,:al_grade;
		printf("%s\t%s\t%ld\n",ac_sname,ac_cname,al_grade);
	}
	EXEC SQL CLOSE score_cursor;
*/

	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}

void score()
{
	int c;
	int flag;

	flag=0;
	while(1)
	{

		score_info();
		scanf("%d",&c);
		switch(c)
		{
		case 1:add_score();break;
		case 2:del_score();break;
		case 3:alter_score();break;
		case 4:find_score();break;
		default:flag=1;break;
		}
		if(1==flag)
			break;
	}
}


void check_course()
{
	system("clear");
	
	toString(23,s_tmp);
	strcpy(info.content,s_tmp);
	if( msgsnd(msgid,(void*)&info,MAX_TEXT,0) == -1)
	{
		fprintf(stderr,"msgsnd failed\n");
		exit(EXIT_FAILURE);
	}

/*
	EXEC SQL CONNECT :v_username IDENTIFIED BY :v_password;

	EXEC SQL DECLARE course_cursor CURSOR FOR
	SELECT cname,ccredit FROM course;

	printf("All courses are as follows:\n");
	
	EXEC SQL WHENEVER NOT FOUND DO break;
	
	EXEC SQL OPEN course_cursor;
	while(1)
	{
		EXEC SQL FETCH course_cursor INTO :ac_cname,:al_ccredit;
		printf("%s\t%ld\n",ac_cname,al_ccredit);
	}
	EXEC SQL CLOSE course_cursor;
*/	
	scanf("%c",&c_tmp);
	scanf("%c",&c_tmp);
}
